package com.xd.center.controller;

import com.alibaba.fastjson.JSON;
import com.xd.center.api.SmartApi;
import com.xd.center.api.bean.DefaultRs;
import com.xd.center.api.bean.SplitAddress;
import com.xd.center.api.bean.request.SmartApiRq;
import com.xd.center.api.bean.response.SmartApiRs;
import com.xd.center.bean.DemoRs;
import com.xd.center.bean.Rq;
import com.xd.center.bean.Rs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/flow/dataflow")
public class XdController {

  @Autowired
  private SmartApi smartApi;

  final String R131V3Service = "GXD_R131V3"; //小区模糊查询接口
  final String R143Service = "GXD_R143"; //小区挂牌房源查询接口
  final String R144Service = "GXD_R144"; //小区成交数据查询接口

  @ResponseBody
  @PostMapping(value = "/getData")
  public Rs getData(@RequestBody Map<String, Object> map) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Rs rs = new Rs();
    rs.setStatus("RDIP-000000");
    rs.setMessage("成功");
    rs.setRequestJson(JSON.toJSONString(map));
    rs.setRequestTimeMine("2021-01-01 10:10:10");
    rs.setResponseTime("2021-01-01 10:11:10");
    rs.setRequestGap(1000);

    HashMap<String, Object> parasMap = (HashMap) map.get("params");
    List parasArray = (List) map.get("paramArray");
    String key = (String) parasMap.get("key");
    String dataflowCode = (String) map.get("dataflowCode");
    SmartApiRs smartApiRs;
    if (CollectionUtils.isEmpty(parasArray)) {
      Method method = smartApi.getClass().getMethod(dataflowCode, HashMap.class, String.class);//得到方法对象,有参的方法需要指定参数类型
      smartApiRs = (SmartApiRs) method.invoke(smartApi, parasMap, key);
    } else {
      Method method = smartApi.getClass().getMethod(dataflowCode, List.class, String.class);//得到方法对象,有参的方法需要指定参数类型
      smartApiRs = (SmartApiRs) method.invoke(smartApi, parasArray, key);
    }

    /*if (dataflowCode.equals("R131V3Service")) { //{\"city\":\"110100\",\"address\":\"怡美家园\",\"houseType\":\"住宅\"}
      smartApiRs = smartApi.R131V3Service(rq, key);
    } else if (dataflowCode.equals("R143Service")) { //{\"residenceId\": \"380247005\",\"area\": \"80\",\"estateType\": \"1\"}
      smartApiRs = smartApi.R143Service(rq, key);
    } else if (dataflowCode.equals("R144Service")) { //{\"residenceid\": \"380247005\",\"area\": \"80\"}
      smartApiRs = smartApi.R144Service(rq, key);
    } else if (dataflowCode.equals("R146V2Service")) { //{\"begindate\": \"202011\",\"endDate\": \"202111\"}
      smartApiRs = smartApi.R146V2Service(rq, key);
    } else if (dataflowCode.equals("R147V2Service")) { //{\"begindate\": \"202011\",\"endDate\": \"202111\"}
      smartApiRs = smartApi.R147V2Service(rq, key);
    }else if (dataflowCode.equals("R148V2Service")) { //{\"begindate\": \"202011\",\"endDate\": \"202111\"}
      smartApiRs = smartApi.R148V2Service(rq, key);
    }else if (dataflowCode.equals("R149Service")) { //{\"residenceId\": \"380247005\"}
      smartApiRs = smartApi.R149Service(rq, key);
    }else if (dataflowCode.equals("R150Service")) { //{\"residenceId\": \"380247005\"}
      smartApiRs = smartApi.R150Service(rq, key);
    }else if (dataflowCode.equals("R151Service")) {
      smartApiRs = smartApi.R151Service(rq, key);
    }*/
    if (null != smartApiRs) rs.setData(smartApiRs);
    return rs;
  }


  @ResponseBody
  @PostMapping(value = "/getToken")
  public Object getToken(@RequestBody Rq rq) {
    Rs rs = new Rs();
    rs.setStatus("RDIP-000000");
    rs.setMessage("成功");
    rs.setRequestJson(JSON.toJSONString(rq));
    rs.setToken("asdasdjkclzjpsdjkcv1230987767");
    DemoRs demoRs = new DemoRs();
    demoRs.setAddress("地址");
    demoRs.setCity("地址");
    rs.setData(demoRs);
    rs.setRequestTimeMine("2021-01-01 10:10:10");
    rs.setResponseTime("2021-01-01 10:11:10");
    rs.setRequestGap(1000);
    return rs;
  }
}
